package com.seven.good.scheduler;

import com.seven.good.dao.SmsremindMapper;
import com.seven.good.pojo.Smsremind;
import com.seven.good.util.SendMail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * <h3>easyauction</h3>
 *
 * @author : 王胜奔
 * @description :
 * @date : 2019-10-17 19:59
 **/
@Component
public class SmsremindScheduler {
    @Autowired
    private SmsremindMapper smsremindMapper;

    @Autowired
    private SendMail sendMail;
//    @Autowired
//    private RedisTemplate redisTemplate;
//    @Scheduled(cron = "0/5 * * * * ?")//定时任务控制，每天12点，24点执行一次
//    public void getSmsreminds(){
//        List<Smsremind> list=smsremindMapper.selectSmsreminds();
//        Map map = new HashMap();
//        for (Smsremind smsremind:list){
//            map.put(smsremind.getSrid().toString(),smsremind);
//        }
//        //向redis缓存里添加一个map
//        redisTemplate.opsForHash().putAll("smsremind",map);
//        //设置键的过期时间
//        redisTemplate.expire("smsremind",4000L, TimeUnit.MILLISECONDS);
//    }



    @Scheduled(cron = "0/5 * * * * ?")//定时任务控制，每5秒钟执行一次
    public void sendSmsremind() {
        //从数据库里查出数据
        List<Smsremind> list=smsremindMapper.selectSmsreminds();
        for (Smsremind smsremind : list) {
            if (smsremind.getsGstate() == 3 && smsremind.getsGendtime().getTime()-30*60*1000 < new Date().getTime()){
                try {
                    //调用发送邮件的方法
                   sendMail.sendEmailVertification(smsremind.getsEmail(),smsremind.getsGtitle(), smsremind.getsGstate());
                    //修改状态或者修改flag=1
                    smsremindMapper.updateFlagBySrid(smsremind.getSrid());
                    System.out.println("发送1");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (smsremind.getsGstate() == 2 && smsremind.getsGstarttime().getTime()-30*60*1000 < new Date().getTime()){
                try {
                    //调用发送邮件的方法
                    sendMail.sendEmailVertification(smsremind.getsEmail(),smsremind.getsGtitle(), smsremind.getsGstate());
                    //修改状态或者修改flag=1
                    smsremindMapper.updateFlagBySrid(smsremind.getSrid());
                    System.out.println("发送2");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
